package org.gcb.core.model.util;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import org.gcb.core.model.Ability;
import org.gcb.core.model.MiscModifier;
import org.gcb.core.model.Skill;
import org.gcb.core.model.Toon;
import org.openide.util.Exceptions;
import org.openide.util.Lookup;
import org.openide.util.lookup.Lookups;

/**
 *
 * @author Jason
 */
public class Utilities {

    public static <T> Collection<MiscModifier<?, ?>> retreiveType(Class<T> aClass) {
	Lookup toonLookup = Lookup.getDefault();
	Toon toon = toonLookup.lookup(Toon.class);
	toon.getMiscModifiers();
	return retrieveType(aClass, toon.getMiscModifiers());
    }

    private Utilities() {
    }

    public static Utilities getInstance() {
        return UtilitiesHolder.INSTANCE;
    }

    private static class UtilitiesHolder {
        private static final Utilities INSTANCE = new Utilities();
    }

    public static <T> Collection<MiscModifier<?, ?>> retrieveType(Class<T> clazz, Collection<MiscModifier<?, ?>> modifiers){
	Set<MiscModifier<?, ?>> result = new HashSet<MiscModifier<?, ?>>(0);
	for (MiscModifier<?, ?> modifier : modifiers) {
	    if (clazz.isInstance(modifier.getType())) {
		result.add((MiscModifier<?, T>) modifier);
	    }
	}
	return result;
    }

    /**
     * Original
     * @param modifiers
     * @return
     */
    public static Integer calculateMiscModifier(Collection<MiscModifier<?, ?>> modifiers) {
        Integer retVal = 0;
        for (MiscModifier<?, ?> miscMod : modifiers) {
            retVal += miscMod.getValue();
        }
        return retVal;
    }

    /**
     * Hoping to use this method as a dynamic calculator for summing up a collection of modifiers. For example, I have:<br>
     * <pre>
     * ...
     * Collection<Buff, Integer> strBuffs;
     * Integer buffValue = Utilities.calculateMiscModifier(Integer.class, strBuffs);
     * ...
     * </pre>
     *
     * The problem is: operator + cannot be applied to T, T<br>
     * @param <T>
     * @param clazz
     * @param modifiers
     * @return
     */
    public static <T> T calculateMiscModifier(Class<T> clazz, Collection<MiscModifier<?, T>> modifiers) {
        Double d;
        T retVal;
        try {
            retVal = clazz.newInstance();
        } catch (InstantiationException ex) {
            retVal = null;
            Exceptions.printStackTrace(ex);
        } catch (IllegalAccessException ex) {
            retVal = null;
            Exceptions.printStackTrace(ex);
        }
        for (MiscModifier<?, T> miscMod : modifiers) {
//            retVal += miscMod.getValueType();
        }
        return retVal;
    }

    /**
     * This method fails in a similar manner.<br>
     * The problem is: operator + cannot be applied to java.lang.Number, capture#1 of ? extends java.lang.Number
     * @param modifiers
     * @return
     */
//    public static Number calculateMiscModifier(Collection<MiscModifier<?, ? extends Number>> modifiers) {
//        Number root = 0;
//        for (MiscModifier<?, ? extends Number> mod : modifiers) {
//            root += mod.getValueType();
//        }
//        return root;
//    }

//    public static Integer sumModifiers(Collection<MiscModifier<?, Integer>> modifiers) {
//        Integer root = 0;
//        for (MiscModifier<?, Integer> mod : modifiers) {
//            root += mod.getValueType();
//        }
//        return root;
//    }

    /**
     * Problem here is that the method signatures are the same. Despite the Type Arguments are different.
     * @param modifiers
     * @return
     */
//    public static Double sumModifiers(Collection<MiscModifier<?, Double>> modifiers) {
//        Double root = 0.0;
//        for (MiscModifier<?, Double> mod : modifiers) {
//            root += mod.getValueType();
//        }
//        return root;
//    }
 }
